package com.luther.design.singleton;

import java.io.ObjectStreamException;

/**
 * 懒汉式(线程安全)
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> luzhenbang
 * <h3>Date</h3> 03/04/2018 12:51
 * <h3>Copyright</h3> Copyright (c)2018 Shenzhen TL  Co., Ltd. Inc. All rights reserved.
 */
public class SingletonLazySyn2 {

    private static SingletonLazySyn2 singleton  = null ;
    /**
     * 构造方法私有化
     * */
    private SingletonLazySyn2(){

    }

    /**
     * 使用关键字synchronized 锁对象
     * @return
     */
    public static  SingletonLazySyn2 getInstance() {
        if (null == singleton){
            synchronized (SingletonLazySyn2.class){
                if (null == singleton){
                    singleton = new SingletonLazySyn2();
                }
            }
        }
        return singleton;
    }

    /**
     * 杜绝单例对象被反序列化时重新生成对象
     * @return
     * @throws ObjectStreamException
     */
    private Object readResolve() throws ObjectStreamException {
        return singleton;
    }
}
